package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.framework.util.PageList;
import com.frogs.system.util.HealthRecordStatusEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.IrritabilityInfoDao;
import com.frogs.ucenter.entity.IrritabilityInfo;
import com.frogs.ucenter.v1.request.IrritabilityInfoDeleteRequest;
import com.frogs.ucenter.v1.request.IrritabilityInfoListRequest;
import com.frogs.ucenter.v1.request.IrritabilityInfoMegerRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.ListResponse;
import com.frogs.ucenter.v1.service.IrritabilityInfoService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION + "irritabilityInfoService")
public class IrritabilityInfoServiceImpl extends BaseServiceImpl<IrritabilityInfo> implements IrritabilityInfoService {
	
	private static final Logger log = LoggerFactory.getLogger(IrritabilityInfoServiceImpl.class);
	
	@Resource
	private IrritabilityInfoDao irritabilityInfoDao;
	
	/**
	 * 添加及修改过敏档案
	 */
	@Override
	public Response mergeRecord(Request request) throws Exception{
		AddResponse<IrritabilityInfo> response = new AddResponse<IrritabilityInfo>();
		try {
			IrritabilityInfoMegerRequest irMergeRequest = (IrritabilityInfoMegerRequest)request;
			
			IrritabilityInfo irritabilityInfo = new IrritabilityInfo();
			Integer id = irMergeRequest.getId();
			irritabilityInfo.setUserId(irMergeRequest.getUserId());
			irritabilityInfo.setDegree(irMergeRequest.getDegree());
			irritabilityInfo.setReaction(irMergeRequest.getReaction());
			irritabilityInfo.setReport(irMergeRequest.getReport());
			irritabilityInfo.setStatus(irMergeRequest.getStatus());
			irritabilityInfo.setType(irMergeRequest.getType());
			irritabilityInfo.setCreateTime(new Date());
			if(id == 0){
				irritabilityInfoDao.insert(irritabilityInfo);
				response.setInfo(irritabilityInfo);
				response.setTip("过敏档案添加成功");
			}else{
				irritabilityInfo.setId(irMergeRequest.getId());
				irritabilityInfoDao.update(irritabilityInfo);
				response.setInfo(irritabilityInfo);
				response.setTip("过敏档案修改成功");
			}
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

	/**
	 * 显示过敏档案
	 */
	@Override
	public Response listRecord(Request request) throws Exception{
		ListResponse<IrritabilityInfo> response = new ListResponse<IrritabilityInfo>();
		try {
			IrritabilityInfoListRequest irListRequest = (IrritabilityInfoListRequest)request;
			Integer userId = irListRequest.getUserId();
			Integer pageNo = irListRequest.getPageNo();
			Integer pageSize = irListRequest.getPageSize();
			Integer status = irListRequest.getStatus();
			
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("eqUserId", userId);
			params.put("eqStatus", status);
			params.put("orderByClause", "create_time desc");
			PageList<IrritabilityInfo> list = new PageList<IrritabilityInfo>();
			list = irritabilityInfoDao.selectByMap(params, pageNo, pageSize);
			response.setList(list);
			response.setTip("过敏档案显示成功");
			
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

	/**
	 * 删除过敏档案(逻辑删除)
	 */
	@Override
	public Response deleteRecord(Request request) throws Exception{
		AddResponse<IrritabilityInfo> response = new AddResponse<IrritabilityInfo>();
		try {
			IrritabilityInfoDeleteRequest irDeleteRequest = (IrritabilityInfoDeleteRequest)request;
			
			IrritabilityInfo irritabilityInfo = new IrritabilityInfo();

			irritabilityInfo.setId(irDeleteRequest.getId());
			irritabilityInfo.setUserId(irDeleteRequest.getUserId());
			irritabilityInfo.setDegree(irDeleteRequest.getDegree());
			irritabilityInfo.setReaction(irDeleteRequest.getReaction());
			irritabilityInfo.setReport(irDeleteRequest.getReport());
			irritabilityInfo.setType(irDeleteRequest.getType());
			irritabilityInfo.setCreateTime(new Date());
			irritabilityInfo.setStatus(HealthRecordStatusEnum.NO.getId());
			
			irritabilityInfoDao.update(irritabilityInfo);
			response.setInfo(irritabilityInfo);
			response.setTip("过敏档案删除成功");
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
	
	
	
}
